package ru.athena.runTool;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import ru.athena.runTool.blockTypes.CodeBlock;
import ru.athena.runTool.blockTypes.Comment;
import sun.swing.WindowsPlacesBar;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RunToolView extends javax.swing.JDialog {

	/**
	 * 
	 */
	
	private static final long serialVersionUID = 5592754528213870554L;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	private AppProperties prop_;
	private DefaultTreeModel treeModel_;	
	private JSplitPane mainSplitPanel;
	private JPanel inputFieldsPanel;
	private JPanel appControlPanel;
	private JButton buttonParse;
	private JTree scriptsTree;
	private JPanel buttonsPanel;
	private JPanel appViewPanel;
	private JButton buttonClose;
	private JButton buttonRunScripts;
	private JTextArea statusTextArea;
	private CustomFileBrowser fileBrowser;
	private JButton buttonStore;
	private JScrollPane treeScrollPane;
	private JScrollPane statusAreaScroll;
	private JSplitPane appViewSplitPanel;
	private JPanel fileBrowserPanel;
	private JPanel spacerPanel;
	private JLabel passwordLabel;
	private JTextField userNameField;
	private JPasswordField passwordField;
	private JLabel userName;
	private JLabel connectLabel;
	private JTextField connectString;
	private JTabbedPane statusAreaTabbedPane_;
	private JScrollPane sourceAreaScroll;
	private JTextArea sourceTextArea;
	private JButton buttonClearLog;
	private JButton buttonParseDir;
	private JTextField selectedFileLabel;
	private JTextArea systemOutTextArea;
	private JScrollPane systemOutAreaScroll;
	private JTextArea systemErrTextArea;
	private JScrollPane systemErrAreaScroll;

	public RunToolView(final Frame parent, final String title,
			final boolean modal) {
		super(parent, title, modal);
		initGUI();
	}
	
	public RunToolView( AppProperties prop ) {
		this( null, "Run Tool", false);
		prop_ = prop;
		getConnectString().setText( prop_.getConnectionString() );
		getUserNameField().setText( prop_.getUserName() );
		try { 
			File	currDir = new File( prop_.getFilePath()).getParentFile();
			getFileBrowser().setCurrentDirectory( currDir );
		} catch (Exception e) {	}
		getSelectedFileTextField().setText( prop_.getFilePath() );
		
	}
	
	private void initGUI() {
		try {
			{
				this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
				this.setPreferredSize(new java.awt.Dimension(695, 423));
				{
					mainSplitPanel = new JSplitPane();
					getContentPane().add(mainSplitPanel, BorderLayout.CENTER);
					mainSplitPanel.setPreferredSize(new java.awt.Dimension(687, 318));
					{
						mainSplitPanel.add(getAppControlPanel(), JSplitPane.RIGHT);
						mainSplitPanel.add(getAppViewPanel(), JSplitPane.LEFT);
					}
				}
			}
			this.setSize(695, 423);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	private JPanel getAppControlPanel() {
		if (appControlPanel == null ) {
			appControlPanel = new JPanel();						
			BoxLayout appControlLayout = new BoxLayout(appControlPanel, javax.swing.BoxLayout.Y_AXIS);
			appControlPanel.setLayout(appControlLayout);
			appControlPanel.setPreferredSize(new java.awt.Dimension(418, 200));
			appControlPanel.add(getFileBrowserPanel());
			appControlPanel.add(getSelectedFilePanel());
			appControlPanel.add(getInputFieldsPanel());
			appControlPanel.add(getButtonsPanel());
		}
		return appControlPanel;
	}
	
	private JTree getScriptsTree() {
		if(scriptsTree == null) {
			treeModel_ = new DefaultTreeModel(
							new DefaultMutableTreeNode(
									new Comment("empty") ));
			scriptsTree = new JTree(treeModel_);
			scriptsTree.getSelectionModel().setSelectionMode(
					TreeSelectionModel.SINGLE_TREE_SELECTION);
			scriptsTree.setShowsRootHandles(true);
			scriptsTree.setPreferredSize(new java.awt.Dimension(500, 50000));
			// tree selection action
			scriptsTree.addTreeSelectionListener( new MyTreeSelectionListener() );			
		}
		return scriptsTree;
	}

	private class MyTreeSelectionListener implements TreeSelectionListener {
		    public void valueChanged(final TreeSelectionEvent e) {
		        final DefaultMutableTreeNode node = (DefaultMutableTreeNode)
		        		getScriptsTree().getLastSelectedPathComponent();
		        /* if nothing is selected */ 
		        if (node == null) return;
		        /* retrieve the node that was selected */
		        CodeBlock	currBlock = (CodeBlock)node.getUserObject();
		        /* React to the node selection. */
		        getSourceTextArea().setText( currBlock.getText() );
		        getStatusAreaScroll().revalidate();
		    }		
	}
	

	private JPanel getInputFieldsPanel() {
		if(inputFieldsPanel == null) {
			inputFieldsPanel = new JPanel();
			GridLayout databasePropPanelLayout = new GridLayout(4, 2, 5, 5);
			databasePropPanelLayout.setColumns(1);
			inputFieldsPanel.setLayout(databasePropPanelLayout);
			inputFieldsPanel.setPreferredSize(new java.awt.Dimension(418, 98));
			inputFieldsPanel.setEnabled(false);
			inputFieldsPanel.setSize(418, 98);
			inputFieldsPanel.setMaximumSize(new java.awt.Dimension(32767, 98));
			inputFieldsPanel.setMinimumSize(new java.awt.Dimension(418, 75));
			inputFieldsPanel.add(getConnectLabel());
			inputFieldsPanel.add(getConnectString());
			inputFieldsPanel.add(getUserNameLabel());
			inputFieldsPanel.add(getUserNameField());
			inputFieldsPanel.add(getPasswordLabel());
			inputFieldsPanel.add(getPasswordField());
		}
		return inputFieldsPanel;
	}

	
	private JButton getButtonParse() {
		if(buttonParse == null) {
			buttonParse = new JButton();
			buttonParse.setText("Parse File");
		}
		return buttonParse;
	}
	
	private JButton getButtonParseDir() {
		if(buttonParseDir == null) {
			buttonParseDir = new JButton();
			buttonParseDir.setText("Parse Dir");
		}
		return buttonParseDir;
	}

	private JTextField getConnectString() {
		if(connectString == null) {
			connectString = new JTextField();
			connectString.setPreferredSize(new java.awt.Dimension(313, 22));
		}
		return connectString;
	}
	
	private JLabel getConnectLabel() {
		if(connectLabel == null) {
			connectLabel = new JLabel();
			connectLabel.setText("Connect");
			connectLabel.setPreferredSize(new java.awt.Dimension(20, 14));
		}
		return connectLabel;
	}
	
	private JLabel getUserNameLabel() {
		if(userName == null) {
			userName = new JLabel();
			userName.setText("User");
			userName.setPreferredSize(new java.awt.Dimension(20, 22));
		}
		return userName;
	}
	
	public JPasswordField getPasswordField() {
		if(passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.setText("service");
			passwordField.setPreferredSize(new java.awt.Dimension(313, 22));
		}
		return passwordField;
	}
	
	private JTextField getUserNameField() {
		if(userNameField == null) {
			userNameField = new JTextField();
			userNameField.setPreferredSize(new java.awt.Dimension(313, 22));
		}
		return userNameField;
	}
	
	private JLabel getPasswordLabel() {
		if(passwordLabel == null) {
			passwordLabel = new JLabel();
			passwordLabel.setText("Password");
			passwordLabel.setPreferredSize(new java.awt.Dimension(20, 14));
		}
		return passwordLabel;
	}
	
	private JTextArea getStatusTextArea() {
		if(statusTextArea == null) {
			statusTextArea = new JTextArea();
			statusTextArea.setPreferredSize(new java.awt.Dimension(254, 83));
	        statusTextArea.setPreferredSize(new Dimension(3000,20000));			
		}
		return statusTextArea;
	}
	
	private JTextArea getSourceTextArea() {
		if(sourceTextArea == null) {
			sourceTextArea = new JTextArea();
	        sourceTextArea.setPreferredSize(new Dimension(3000,20000));
		}
		return sourceTextArea;
	}

	public void activateStartButton(Boolean isActive) {
		buttonParse.setEnabled(isActive);
	}

	public void addRunActionListener(ActionListener l) {
		buttonRunScripts.addActionListener(l);
	}

	public void addParseActionListener(ActionListener l) {
		buttonParse.addActionListener(l);
	}

	public void addParseDirActionListener(ActionListener l) {
		buttonParseDir.addActionListener(l);
	}

	public void addExitActionListener( ActionListener l) {
		buttonClose.addActionListener(l);
//	    this.addWindowListener( l );				
		
	}
	
	public String getConnectionString() {
		return connectString.getText();
	}

	public String getChoosedPath() {
		return getSelectedFileTextField().getText();
	}

	public TreePath getTreeSelectionPath() {
		TreePath parentPath = scriptsTree.getSelectionPath();
		if (parentPath != null) { 
			return parentPath;
		}
		//There is no selection. Default to the root node.
		return new TreePath( treeModel_.getRoot()); 
	}

	public void setRootNode(DefaultMutableTreeNode root) {
		treeModel_.setRoot(root);
	}

	public void setSelectedNode(TreePath path) {
		getScriptsTree().clearSelection();
		getScriptsTree().addSelectionPath( path );
		getScriptsTree().scrollPathToVisible(path );
		
	}

	public void showError(String errMessage) {
		JOptionPane.showMessageDialog(this, errMessage);		
	}
	
	private JButton getButtonRunScripts() {
		if(buttonRunScripts == null) {
			buttonRunScripts = new JButton();
			buttonRunScripts.setText("Execute");
			buttonRunScripts.setPreferredSize(new java.awt.Dimension(81, 23));
		}
		return buttonRunScripts;
	}
	
	private JButton getButtonClose() {
		if(buttonClose == null) {
			buttonClose = new JButton();
			buttonClose.setText("Exit");
			buttonClose.setPreferredSize(new java.awt.Dimension(81, 23));
		}
		return buttonClose;
	}
	
	public String getUserName() {	
		return userNameField.getText();
	}
	
	public String getPasword() {	
		return new String( passwordField.getPassword());
	}
	
	private JPanel getAppViewPanel() {
		if(appViewPanel == null) {
			appViewPanel = new JPanel();
			GridLayout appViewPanelLayout = new GridLayout(1, 1, 5, 5);
			appViewPanelLayout.setColumns(1);
			appViewPanel.setLayout(appViewPanelLayout);
			appViewPanel.setPreferredSize(new java.awt.Dimension(262, 138));			
			appViewPanel.add(getAppViewSplitPanel());
		}
		return appViewPanel;
	}

	private JPanel getButtonsPanel() {
		if(buttonsPanel == null) {
			buttonsPanel = new JPanel();
			BoxLayout buttonsPanelLayout = new BoxLayout(buttonsPanel, javax.swing.BoxLayout.X_AXIS);
			buttonsPanel.setLayout(buttonsPanelLayout);
			buttonsPanel.setPreferredSize(new java.awt.Dimension(347, 23));
			buttonsPanel.add(getButtonClearLog());
			buttonsPanel.add(getButtonParse());
			buttonsPanel.add(getButtonParseDir());			
			buttonsPanel.add(getButtonRunScripts());
			buttonsPanel.add(getButtonStore());
			buttonsPanel.add(getButtonClose());

		}
		return buttonsPanel;
	}

	private JButton getButtonClearLog() {
		if(buttonClearLog == null) {
			buttonClearLog = new JButton();
			buttonClearLog.setText("Clear Status");
			buttonClearLog.addActionListener( 
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						getStatusTextArea().setText("");
						getSystemErrTextArea().setText("");
						getSystemOutTextArea().setText("");
					}
				}
			);
		}
		return buttonClearLog;
	}

	private JPanel getSelectedFilePanel() {
		if(spacerPanel == null) {
			spacerPanel = new JPanel();
			spacerPanel.setPreferredSize(new java.awt.Dimension(418, 20));
			spacerPanel.setMaximumSize(new java.awt.Dimension(32767, 20));
			BorderLayout theLayout = new BorderLayout();
			spacerPanel.setLayout(theLayout);
			spacerPanel.add( getSelectedFileTextField());
		}
		return spacerPanel;
	}
	private void setSelectedFileLabel( String fileName ) {
		getSelectedFileTextField().setText( fileName );
	}
	
	private JTextField getSelectedFileTextField() {
		if (selectedFileLabel == null ) {
			selectedFileLabel = new JTextField();
			selectedFileLabel.setEnabled(false);
		}
		return selectedFileLabel;
	}

	private JPanel getFileBrowserPanel() {
		if(fileBrowserPanel == null) {
			fileBrowserPanel = new JPanel();
			BorderLayout theLayout = new BorderLayout();
			fileBrowserPanel.setLayout(theLayout);
			fileBrowserPanel.setPreferredSize(new java.awt.Dimension(435, 186));
			fileBrowserPanel.add(getFileBrowser(), BorderLayout.CENTER);
		}
		return fileBrowserPanel;
	}

	private JSplitPane getAppViewSplitPanel() {
		if(appViewSplitPanel == null) {
			appViewSplitPanel = new JSplitPane();
			appViewSplitPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
			appViewSplitPanel.add(getStatusTabbedPane(), JSplitPane.BOTTOM);
			appViewSplitPanel.add(getTreeScrollPane(), JSplitPane.TOP);
		}
		return appViewSplitPanel;
	}

	private JTabbedPane getStatusTabbedPane() {
		if (statusAreaTabbedPane_ == null ) {
			statusAreaTabbedPane_ = new JTabbedPane(JTabbedPane.TOP);
			statusAreaTabbedPane_.setPreferredSize(new java.awt.Dimension(260, 93));
			statusAreaTabbedPane_.addTab("Log", getStatusAreaScroll());
			statusAreaTabbedPane_.addTab("Source", getSourceAreaScroll());
			statusAreaTabbedPane_.addTab("System Log", getSystemOutAreaScroll());
			statusAreaTabbedPane_.addTab("Errors", getSystemErrAreaScroll());
		}
		return statusAreaTabbedPane_;
	}

	private JScrollPane getSourceAreaScroll() {
		if(sourceAreaScroll == null) {
			sourceAreaScroll = new JScrollPane();
			sourceAreaScroll.setPreferredSize(new java.awt.Dimension(260, 93));
			sourceAreaScroll.setViewportView(getSourceTextArea());
		}
		return sourceAreaScroll;
	}
	
	private JScrollPane getStatusAreaScroll() {
		if(statusAreaScroll == null) {
			statusAreaScroll = new JScrollPane();
			statusAreaScroll.setPreferredSize(new java.awt.Dimension(260, 93));
			statusAreaScroll.setViewportView(getStatusTextArea());
		}
		return statusAreaScroll;
	}

	private JScrollPane getSystemOutAreaScroll() {
		if(systemOutAreaScroll == null) {
			systemOutAreaScroll = new JScrollPane();
			systemOutAreaScroll.setPreferredSize(new java.awt.Dimension(260, 93));
			systemOutAreaScroll.setViewportView(getSystemOutTextArea());
		}
		return systemOutAreaScroll;
	}
	
	private JScrollPane getSystemErrAreaScroll() {
		if(systemErrAreaScroll == null) {
			systemErrAreaScroll = new JScrollPane();
			systemErrAreaScroll.setPreferredSize(new java.awt.Dimension(260, 93));
			systemErrAreaScroll.setViewportView(getSystemErrTextArea());
		}
		return systemErrAreaScroll;
	}
		
	private JScrollPane getTreeScrollPane() {
		if(treeScrollPane == null) {
			treeScrollPane = new JScrollPane();
			treeScrollPane.setPreferredSize(new java.awt.Dimension(260, 199));
			treeScrollPane.setViewportView(getScriptsTree());
		}
		return treeScrollPane;
	}

	public void addStoreListener(ActionListener l) {
		buttonStore.addActionListener(l);
		
	}
	
	private JButton getButtonStore() {
		if(buttonStore == null) {
			buttonStore = new JButton();
			buttonStore.setText("Save Tree");
		}
		return buttonStore;
	}

	public DefaultTreeModel getTreeModel() {
		return treeModel_;
	}
	
	private CustomFileBrowser getFileBrowser() {
		if(fileBrowser == null) {
			fileBrowser = new CustomFileBrowser();
			fileBrowser.setPreferredSize(new java.awt.Dimension(419, 242));
			fileBrowser.addChangeSelectionListener( new PropertyChangeListener() {
				public void propertyChange(PropertyChangeEvent evt) {
					try {
						File f = fileBrowser.getSelectedFile();
						if (f != null) {
							setSelectedFileLabel( fileBrowser.getSelectedFile().getCanonicalPath() );							
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
			});
		}
		return fileBrowser;
	}

	public void addStatusText(String text) {
		getStatusTextArea().append( text );		
	}

	public JTextArea getSystemOutTextArea() {
		if ( systemOutTextArea == null ) {
			systemOutTextArea = new JTextArea();
			systemOutTextArea.setPreferredSize(new Dimension(3000,20000));			
		}
		return systemOutTextArea;
	}

	public JTextArea getSystemErrTextArea() {
		if ( systemErrTextArea == null ) {
			systemErrTextArea = new JTextArea();
			systemErrTextArea.setPreferredSize(new Dimension(3000,20000));			
		}
		return systemErrTextArea;
	}
	
}
